package servlet;

import com.alibaba.fastjson.JSON;
import dao.DAO;
import domain.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/BasketServlet")
public class BasketServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String type=request.getParameter("type");
        if("findAll_basket".equals(type)){
            getAllBasket(request,response);
        }else if ("add".equals(type)){
            basket_add(request,response);
        }else if ("subtraction".equals(type)){
            basket_subtraction(request,response);
        }else if ("check".equals(type)){
            check_money(request,response);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    DAO dao = new DAO();
    public void getAllBasket(HttpServletRequest request, HttpServletResponse response) {
        ArrayList<Sale_goods> basketGoods = dao.getAllBaskets();
        String listStr = JSON.toJSONString(basketGoods);
        try {
            response.getWriter().print(listStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void basket_add(HttpServletRequest request, HttpServletResponse response) {

        int id=Integer.parseInt(request.getParameter("id"));
        int result = 0;
        result = dao.addAmountOfBasketGoods(id);
        response.setCharacterEncoding("utf-8");
        PrintWriter out= null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(result == 1){
            out.print("增加成功!");
        }else{
            out.print("增加失败！");
        }

        out.flush();
        out.close();
    }
    public void basket_subtraction(HttpServletRequest request, HttpServletResponse response) {
//        String id_detail=request.getParameter("id");
//        int id_int=Integer.parseInt(id_detail);
//        //List<Goods> list = goodsDao.findByID_goods(id_int);
//        //String listStr = JSON.toJSONString(list);
//        //System.out.println(listStr);
//
//        try {
//            response.getWriter().print(listStr);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        int id=Integer.parseInt(request.getParameter("id"));
        int result = 0;
        result = dao.subAmountOfBasketGoods(id);
        response.setCharacterEncoding("utf-8");
        PrintWriter out= null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(result == 1){
            out.print("减少成功!");
        }else{
            out.print("减少失败！");
        }

        out.flush();
        out.close();
    }

public void check_money(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        int result = 0;
        DAO basketDao = new DAO();
        DAO userDao= new DAO();

        ArrayList<Basket> baskets = basketDao.getAllBaskets();

        String[] ids = request.getParameterValues("baskets_id");
        for(int i = 0;i < ids.length;i++) {
            int id = Integer.parseInt(ids[i]);
            basketDao.checkBaskets(id);//传购物车内商品的id，清空购物车
        }

        User user = new User();

        int user_id = Integer.parseInt(request.getParameter("user_id"));
        result = userDao.createOrder(user_id);//利用用户id生成新的订单

        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (result>0) {
            out.print("订单添加成功&从购物车中移除");
        } else {
            out.print("订单添加失败");

            out.flush();
            out.close();

        }
    }
}
